/**
 * @description  按债券类别（归因分析）
 * @author shenhong
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { ThreeHeaderC, XYAxisChartsC } from '../../../../comps';
import style from '../index.less';
import _ from 'lodash';

class BondType extends PureComponent {
  constructor() {
    super();
    this.state = {};
  }
  componentDidMount() {
    const { dispatch, args } = this.props;
    const data = _.cloneDeep(args);
    data.classification = 'bond_category';
    dispatch({
      type: 'attributionAnalysisM/getBondType',
      payload: data,
    });
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    const { dispatch, args } = nextProps;
    if (JSON.stringify(args) !== JSON.stringify(this.props.args)) {
      const data = _.cloneDeep(args);
      data.classification = 'bond_category';
      dispatch({
        type: 'attributionAnalysisM/save',
        payload: { BondType: { xAxis: undefined, series: undefined } },
      });
      dispatch({
        type: 'attributionAnalysisM/getBondType',
        payload: data,
      });
    }
  }

  // 对series做调整、设置双轴及第二种图的类型
  changeSeries = (arr) => {
    let data = _.cloneDeep(arr);
    if (data && data.length) {
      data.map((item) => {
        if (item.name == '资产占比') {
          item.yAxisIndex = 1;
          item.type = 'line';
        }
      });
    }
    return data;
  };

  showCharts = () => {
    const {
      attributionAnalysisM: { BondType },
    } = this.props;
    const xAxis = BondType.xAxis;
    const series = BondType.series;
    return (
      <div style={{ height: '325px' }}>
        <XYAxisChartsC series={series} xAxis={xAxis} height={325} yAxisUnit={['单位(%)', '单位(%)']} type='barStack' yAxisRange={BondType.yAxisRange} />
      </div>
    );
  };

  render() {
    return (
      <div className={style.back_charts}>
        <ThreeHeaderC title='按债券类别' />
        <div style={{ height: '325px' }} className='echarts'>
          {this.showCharts()}
        </div>
      </div>
    );
  }
}
export default connect(({ attributionAnalysisM }) => ({
  attributionAnalysisM,
}))(BondType);
